#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;


const int N = 1e5 +10;
string strs[N];
string str1, str2;

// 想了想，这个好像是可以使用栈来解决的
//? 使用 stack + 贪心
stack<int> s1, s2;
int ret = 1e5 + 10;
void update()
{
    ret = std::min(ret, static_cast<int>(fabs(s1.top() - s2.top())));
}
int main()
{
    int n; cin >> n;
    cin >> str1 >> str2;
    if (str1.empty() || str2.empty()) {
        cout << -1 << endl;
        return 0;
    }

    bool f1 = false, f2 = false;
    for (int i = 1; i <= n; i++) {
        cin >> strs[i];
        if (strs[i] == str1) f1 = true;
        if (strs[i] == str2) f2 = true;
    }

    if (!f1 || !f2) {
        cout << -1 << endl;
        return 0;
    }

    for (int i = 1; i <= n; i++)
    {
        if (strs[i] == str1) s1.push(i);
        if (strs[i] == str2) s2.push(i);

        if (s1.size() && s2.size()){
            update();
        }
    }
    cout << ret << endl;
    return 0;
}